package com.sgb.kjwl.viewmodel.transport;

import android.app.Application;
import android.content.Context;
import android.util.ArrayMap;

import androidx.annotation.NonNull;
import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.MutableLiveData;

import com.sgb.kjwl.api.NewAPIManager;
import com.sgb.kjwl.model.entity.transport.AttachmentListEntity;
import com.sgb.kjwl.model.entity.transport.TeamRespVoEntity;
import com.sgb.kjwl.model.response.ListResponseV1;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.base.model.entity.ListResponse;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.MToast;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * author:熊丽
 * createDate:2021/10/9 17:26
 * introduce:
 * modifier:xxx
 * modifiyDate:
 * modifiyContent:
 */
public class InvitationFleetModel extends AndroidViewModel {
    public MutableLiveData<ListResponse<List<AttachmentListEntity>>> ldlist = new MutableLiveData<>();
    public MutableLiveData<Integer> ldadd = new MutableLiveData<>();
    public InvitationFleetModel(@NonNull Application application) {
        super(application);
    }
    /**
     * 获取货源列表
     */
    public void getInvitationList(Context context,String teamName, int page, int state) {
        DialogHelper.showProgressDialog(context, "加载中", 0, false);
        NewAPIManager.getInstance().getLogisticsAPI().getselectByTeamName(teamName,10,page).enqueue(new Callback<BaseEntity<ListResponse<List<AttachmentListEntity>>>>() {
            @Override
            public void onResponse(Call<BaseEntity<ListResponse<List<AttachmentListEntity>>>> call, Response<BaseEntity<ListResponse<List<AttachmentListEntity>>>> response) {
                DialogHelper.dismissProgressDialog();
               if (response.body()==null){
                   ldlist.postValue(new ListResponse<>());
                   return;
               }
                ldlist.postValue(response.body().getData());
            }

            @Override
            public void onFailure(Call<BaseEntity<ListResponse<List<AttachmentListEntity>>>> call, Throwable t) {
                ldlist.postValue(new ListResponse(false));
            }
        });
    }
    public void addOrUpdateAnchor(Context context, TeamRespVoEntity teamRespVoEntity,int position){
        Map<String , Object> map = new ArrayMap<>();
        map.put("id",teamRespVoEntity.getId());
        map.put("motorcadeName",teamRespVoEntity.getName());
        map.put("motorcadeNo",teamRespVoEntity.getNo());
        map.put("name",teamRespVoEntity.getContact());
        map.put("telphone",teamRespVoEntity.getPhone());
        NewAPIManager.getInstance().getLogisticsAPI().addOrUpdateAnchor(map).enqueue(new Callback<BaseEntity>() {
            @Override
            public void onResponse(Call<BaseEntity> call, Response<BaseEntity> response) {
                if (response.body()!=null){
                    if ("200".equals(response.body().getCode())){
                        ldadd.postValue(position);
                    }else {
                        MToast.showToast(context,"操作失败，请稍后重试");
                    }
                }else {
                    MToast.showToast(context,"操作失败，请稍后重试");
                }
            }

            @Override
            public void onFailure(Call<BaseEntity> call, Throwable t) {
                ldadd.postValue(0);
                MToast.showToast(context,"操作失败，请稍后重试");
            }
        });
    }
}
